package com.wll.talker;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.multidex.BuildConfig;
import android.util.Log;
import android.widget.Toast;

import com.igexin.sdk.IUserLoggerInterface;
import com.igexin.sdk.PushManager;
import com.wll.common.app.Application;
import com.wll.factory.Factory;
import com.wll.factory.data.helper.AccountHelper;
import com.wll.factory.persistence.Account;
import com.wll.talker.activities.AccountActivity;

/**
 * created by detachment on 2020/5/8
 */
public class App extends Application {

    private static final String TAG = "GetuiSdkDemo";

    public static String isCIDOnLine = "";
    public static String cid = "";
    public static Context appContext;
    /**
     * 应用未启动, 个推 service已经被唤醒,保存在该时间段内离线消息
     */
    public static StringBuilder payloadData = new StringBuilder();
    private static DemoHandler handler;

    public static void sendMessage(Message msg) {
        handler.sendMessage(msg);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        // 调用Factory进行初始化
        Factory.setup();

        appContext = this;
        Log.d(TAG, "DemoApplication onCreate");

        Config.init(this);
        if (handler == null) {
            handler = new DemoHandler();
        }
        initSdk();

        // 推送进行初始化
//        PushManager.getInstance().initialize(this);
    }

    private void initSdk() {
        Log.d(TAG, "initializing sdk...");
        PushManager.getInstance().initialize(this);
        if (BuildConfig.DEBUG) {
            //切勿在 release 版本上开启调试日志
            PushManager.getInstance().setDebugLogger(this, new IUserLoggerInterface() {
                @Override
                public void log(String s) {
                    Log.i("PUSH_LOG", s);
                }
            });
        }
    }

    @Override
    protected void showAccountView(Context context) {
        // 登录界面的显示
        AccountActivity.show(context);
    }


    public static class DemoHandler extends Handler {
        public static final int RECEIVE_MESSAGE_DATA = 0; //接收到消息
        public static final int RECEIVE_CLIENT_ID = 1; //cid通知
        public static final int RECEIVE_ONLINE_STATE = 2; //cid在线状态通知消息
        public static final int SHOW_TOAST = 3; //Toast消息

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case RECEIVE_MESSAGE_DATA:  //接收到消息
                    payloadData.delete( 0, payloadData.length() );
                    payloadData.append((String) msg.obj);
                    payloadData.append("\n");
                    Log.i("RECEIVE_MESSAGE_DATA", String.valueOf(payloadData));
                    onMessageArrived(String.valueOf(payloadData));
                    break;
                case RECEIVE_CLIENT_ID:  //cid通知
                    cid = (String) msg.obj;
                    Log.i("RECEIVE_CLIENT_ID", cid);
                    onClientInit(cid);
                    break;
                case RECEIVE_ONLINE_STATE:  //cid在线状态通知
                    isCIDOnLine = (String) msg.obj;
                    Log.i("RECEIVE_ONLINE_STATE", msg.obj.equals("true") ?
                            appContext.getResources().getString(R.string.online) :
                            appContext.getResources().getString(R.string.offline));
                    break;
                case SHOW_TOAST:  //需要弹出Toast
                    Toast.makeText(appContext, (String) msg.obj, Toast.LENGTH_SHORT).show();
                    Log.i("SHOW_TOAST", (String) msg.obj);
                    break;
            }
        }
    }

    /**
     * 当Id初始化的试试
     *
     * @param cid 设备Id
     */
    private static void onClientInit(String cid) {
        // 设置设备Id
        Account.setPushId(cid);
        if (Account.isLogin()) {
            // 账户登录状态，进行一次PushId绑定
            // 没有登录是不能绑定PushId的
            AccountHelper.bindPush(null);
        }
    }

    /**
     * 消息达到时
     *
     * @param message 新消息
     */
    private static void onMessageArrived(String message) {
        // 交给Factory处理
        Factory.dispatchPush(message);
        Log.d("消息到达：", message);
    }
}

